// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.parts;

import java.util.ArrayList;
import java.util.List;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.model.database.AbstractDBObject;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

public abstract class AbstractDBGraphicalEditPart extends AbstractGraphicalEditPart implements Adapter {

    public AbstractDBGraphicalEditPart() {
    }

    protected void createNodeParameters() {
        propParameters = new ArrayList<NodePropertyParameter>();
        createPropertyParameters();
        createMainParameters();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPropertyParameters".
     * 
     * @return
     */
    protected void createPropertyParameters() {

    }

    protected void createMainParameters() {

    }

    @Override
    public void activate() {
        if (!isActive()) {
            if (getModel() != null) {
                getCastModel().eAdapters().add(this);
            }
            super.activate();
        }
    }

    public abstract AbstractDBObject getCastModel();

    @Override
    public void deactivate() {
        if (isActive()) {
            if (getModel() != null) {
                getCastModel().eAdapters().remove(this);
            }
            super.deactivate();
        }
    }

    private Notifier target;

    public Notifier getTarget() {
        return target;
    }

    public void setTarget(Notifier newTarget) {
        this.target = newTarget;
    }

    public boolean isAdapterForType(Object type) {
        return (getModel().getClass() == type);
    }

    protected List<NodePropertyParameter> propParameters;

    /**
     * Getter for propParameters.
     * 
     * @return the propParameters
     */
    public List<NodePropertyParameter> getPropParameters() {
        return this.propParameters;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getElementParameter".
     * 
     * @param propName
     * @return
     */
    public NodePropertyParameter getElementParameter(String propName) {
        for (NodePropertyParameter pa : propParameters) {
            if (pa.getName().equals(propName)) {
                return pa;
            }
        }
        return null;
    }
}
